﻿<%@ WebHandler Language="C#" Class="MutiUploadHandler" %>

using System;
using System.Web;
using System.IO;
using ExtBusiness.Common;
using System.Web.SessionState;

public class MutiUploadHandler : IHttpHandler
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";

        HttpPostedFile file = context.Request.Files["Filedata"];
        //string uploadPath =HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

        if (file != null && !string.IsNullOrEmpty(context.Request["userid"]))
        {
            //if (!Directory.Exists(uploadPath))
            //{
            //    Directory.CreateDirectory(uploadPath);
            //}
            //file.SaveAs(uploadPath + file.FileName);
            //SaveAs(uploadPath + file.FileName, file);
            
            AttachmentFileUpload ObjFile = new AttachmentFileUpload();
            if (ObjFile.readfile(file))
            {
                //修正路径及文件名
                string strFileExt = file.FileName.Substring(file.FileName.LastIndexOf(".")+1);
                string strFileName = file.FileName.Substring(0,file.FileName.LastIndexOf("."));
                
                string strNodeName = string.Format("({0}){1}", strFileExt, strFileName);
                string strNodePath = "";
                if (context.Request["ml"].Equals("/"))
                {
                    strNodePath = strNodeName;
                }
                else//选择其他目录
                {
                    strNodePath = context.Request["ml"] + "/" + strNodeName;
                }
                
                //插入数据库
                if (!ObjFile.Update_St_Attachment(context.Request["IID"], strNodePath, strNodeName, context.Request["userid"], context.Request["step"]))
                {
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                    context.Response.Write("1");
                }
            }
            context.Response.Write("0");
        }
        else
        {
            context.Response.Write("0");
        }
    }

    
    //断点续传保存
    private void SaveAs(string saveFilePath, HttpPostedFile file)
    {
        long lStartPos = 0;
        int startPosition = 0;
        int endPosition = 0;
        var contentRange = HttpContext.Current.Request.Headers["Content-Range"];
        //bytes 10000-19999/1157632
        if (!string.IsNullOrEmpty(contentRange))
        {
            contentRange = contentRange.Replace("bytes", "").Trim();
            contentRange = contentRange.Substring(0, contentRange.IndexOf("/"));
            string[] ranges = contentRange.Split('-');
            startPosition = int.Parse(ranges[0]);
            endPosition = int.Parse(ranges[1]);
        }
        System.IO.FileStream fs;
        if (System.IO.File.Exists(saveFilePath))
        {
            fs = System.IO.File.OpenWrite(saveFilePath);
            lStartPos = fs.Length;

        }
        else
        {
            fs = new System.IO.FileStream(saveFilePath, System.IO.FileMode.Create);
            lStartPos = 0;
        }
        if (lStartPos > endPosition)
        {
            fs.Close();
            return;
        }
        else if (lStartPos < startPosition)
        {
            lStartPos = startPosition;
        }
        else if (lStartPos > startPosition && lStartPos < endPosition)
        {
            lStartPos = startPosition;
        }
        fs.Seek(lStartPos, System.IO.SeekOrigin.Current);
        byte[] nbytes = new byte[512];
        int nReadSize = 0;
        nReadSize = file.InputStream.Read(nbytes, 0, 512);
        while (nReadSize > 0)
        {
            fs.Write(nbytes, 0, nReadSize);
            nReadSize = file.InputStream.Read(nbytes, 0, 512);
        }
        fs.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}